package com.zl.csMall.mine.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zl.csMall.mine.order.mapper.OrderMapper;
import com.zl.csMall.mine.order.po.Orders;
import com.zl.csMall.mine.order.service.OrderService;
import com.zl.csMall.mine.recAddress.po.RecAddress;
import com.zl.csMall.mine.recAddress.service.RecAddressService;
import com.zl.csMall.product.commodity.po.Commodity;
import com.zl.csMall.product.commodity.service.CommodityService;
import com.zl.csMall.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description: OrderServiceImpl
 * @author: ChenNing
 * @date 2025-10-22 10:09
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {

    @Autowired
    private CommodityService commodityService;
    @Autowired
    private RecAddressService recAddressService;


    @Override
    public boolean addOrder(Orders orders) {
        orders.setStatus(2);
        orders.setCreateDate(new Date());
        return this.save(orders);
    }

    @Override
    public Orders getOrders(long id) {
        Orders orders = this.getById(id);
        if (orders == null || orders.getId() == 0){
            return null;
        }
        Map<String, Object> commodity = commodityService.getSimpleCommodity(orders.getCommodityId());
        orders.setCommodity(commodity);
        RecAddress recAddress = recAddressService.getById(orders.getRecAddressId());
        orders.setRecAddress(recAddress);
        return orders;
    }

    @Override
    public Page<Orders> getOrderAll(LambdaQueryWrapper<Orders> queryWrapper, Page<Orders> page, String content) {
        // 判断是否有商品查询条件
        if (!Util.isNullOrEmpty(content)){
            LambdaQueryWrapper<Commodity> commodityQuery = new LambdaQueryWrapper<>();
            commodityQuery.select(Commodity::getId).like(Commodity::getNumber, content).like(Commodity::getName, content);
            List<Map<String, Object>> commodityIdList = commodityService.listMaps(commodityQuery);
            int commodityIndex = 0;
            for (Map<String, Object> commodityMap : commodityIdList){
                if (commodityIndex == 0){
                    queryWrapper.eq(Orders::getCommodityId, commodityMap.get("id"));
                } else {
                    queryWrapper.or().eq(Orders::getCommodityId, commodityMap.get("id"));
                }
                commodityIndex++;
            }
        }
        // 订单查询
        page = this.page(page, queryWrapper);
        List<Orders> ordersList = page.getRecords();
        int index = 0;
        for (Orders orders : ordersList){
            // 商品处理
            if (orders.getCommodityId() != null){
                Map<String, Object> commodityMap = commodityService.getSimpleCommodity(orders.getCommodityId());
                orders.setCommodity(commodityMap);
            }
            ordersList.set(index, orders);
            index++;
        }
        return page.setRecords(ordersList);
    }

}
